﻿<cfsilent>
	<cfsetting requesttimeout="300" />
	<cfscript>
		
		propAdvice = getProperty("serviceFactory").getBean("studentPropertyAdvice");
		imgObj = getProperty("serviceFactory").getBean("imageObject");
		fileObj = getProperty("serviceFactory").getBean("tagFileHelper");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		picSuccess = 0;
		picFailure = 0;
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		errorLog = [];
		
		fileNames = event.getArg("fileNames");
		
		if ( len(fileNames) ) {
			
			fileArray = listToArray( fileNames, "*" );

			transaction {
				
				for ( picName in fileArray ) {
					
					studentId = listFirst( picName, "." );
					uploadFile = getProperty("cdnAssetsPath") & "/tmp/stuPicture/" & picName;
					
					sql = "SELECT a.student_prop 
							FROM t_student_status a 
							WHERE a.stu_id = :studentId ";
							
					queryObj = new query( datasource=application.dnsMaster);
					queryObj.addParam( name="studentId", value=studentId , cfsqltype="cf_sql_varchar" );
					
					rs_student = queryObj.execute( sql=sql ).getResult();

					if ( rs_student.recordCount GT 0 ) {
						
						/* 进行学生图片处理逻辑 */
						if ( fileExists(uploadFile) ) {
							
							try {

								portraitDir = getProperty("cdnAssetsPath") & "/student/portrait/" & left( studentId, 4 ) & "/";
								profileDir = getProperty("cdnAssetsPath") & "/student/profile/" & left( studentId, 4 ) & "/";
								printDir = getProperty("cdnAssetsPath") & "/student/print/" & left( studentId, 4 ) & "/";
					
								if ( not directoryExists( portraitDir ) ) {
									directoryCreate( portraitDir );
								}
								if ( not directoryExists( profileDir ) ) {
									directoryCreate( profileDir );
								}
								if ( not directoryExists( printDir ) ) {
									directoryCreate( printDir );
								}
							

								/* 小头像 50x50 */
								imgObj.readImage( uploadFile );
	
								imgObj.scaleWidth( 152 );
								imgObj.blur( 1 );
								imgObj.crop( 1, max( (imgObj.getHeight()-150)/2 - 15, 1 ), 150, 150 );
								imgObj.scaleWidth( 50 );
							
								if ( fileExists( portraitDir & studentId & ".jpg" ) ) {
									fileObj.delete( portraitDir & studentId & ".jpg" );
								}
							
								imgObj.writeImage( portraitDir & studentId & ".jpg", "jpg", 100);
								
								/* 将图片对象放置到系统缓存 */
								binaryStream = fileObj.readBinary( portraitDir & studentId & ".jpg" );
								cachePut( "user_portrait_" & studentId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
								

								/* 证件照 135x180 */
								imgObj.readImage( uploadFile );

								imgObj.scaleWidth( 405 );
								imgObj.blur( 1 );
								imgObj.scaleWidth( 137 );
								imgObj.crop( 1, (imgObj.getHeight()-180)/2, 135, 180 );
							
								if ( fileExists( profileDir & studentId & ".jpg" ) ) {
									fileObj.delete( profileDir & studentId & ".jpg" );
								}
							
								imgObj.writeImage( profileDir & studentId & ".jpg", "jpg", 100);
								
								/* 将图片对象放置到系统缓存 */
								binaryStream = fileObj.readBinary( profileDir & studentId & ".jpg" );
								cachePut( "user_profile_" & studentId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );

							
								/* 打印照片 480x640 */
								imgObj.readImage( uploadFile );
	
								imgObj.scaleWidth( 484 );
								imgObj.crop( 2, (imgObj.getHeight()-640)/2, 480, 640 );
							
								if ( fileExists( printDir & studentId & ".jpg" ) ) {
									fileObj.delete( printDir & studentId & ".jpg" );
								}

								imgObj.writeImage( printDir & studentId & ".jpg", "jpg", 100);
								
								/* 将图片对象放置到系统缓存 */
								binaryStream = fileObj.readBinary( printDir & studentId & ".jpg" );
								cachePut( "user_print_" & studentId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
								
								/* 删除临时文件 */
								fileDelete( uploadFile );
								
								picSuccess++;
							
							}
							catch ( any expt ) {
								
								picFailure++;
								
								/* 删除无法处理的文件 */
								fileDelete( uploadFile );
								
								arrayAppend( errorLog, picName & " 文件格式错误" );
							
							}
							
						}

						propAdvice.parseProperty( rs_student.student_prop );
						
						/* 若各类照片都存在 则刷新学生状态 */
						if ( fileExists( portraitDir & studentId & ".jpg" ) and fileExists( profileDir & studentId & ".jpg" ) and fileExists( printDir & studentId & ".jpg" ) ) {
							propAdvice.setHasPicture( true );
						}
						else {
							propAdvice.setHasPicture( false );
						}
						
						propStr = propAdvice.stringProperty();
						
						sql = "UPDATE t_student_status a SET a.student_prop = :propStr WHERE a.stu_id = :studentId ";
						
						queryObj = new query( datasource=application.dnsMaster);
						queryObj.addParam( name="studentId", value=studentId , cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="propStr", value=propStr , cfsqltype="cf_sql_varchar" );
						
						queryObj.execute( sql=sql );
						
					}
					else {
						
						picFailure++;
						
						/* 删除不符合命名规则的文件 
						if ( fileExists(uploadFile) ) {
							fileDelete( uploadFile );
						}*/

						arrayAppend( errorLog, picName & " 无法使用 学号 规则进行学籍档案匹配" );
						
					}
					
				}
				
				structInsert(response, "operation", "本次处理 "& arrayLen(fileArray) &" 张图片, 成功追加学籍电子照片 "& picSuccess &" 张, 无效图片 "& picFailure &" 张", true);
				structInsert(response, "errorLog", errorLog, true);
				structInsert(response, "success", true, true);
			}
			
			
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>